package demo1;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-09-23
 * Time: 21:45
 */
public class Solution {
    public int romanToInt(String s) {
        int sum = 0;
        for (int i = 0; i < s.length(); i++) {
            int cur = getInt(s.charAt(i));
            if (i < s.length()-1) {
                int next = getInt(s.charAt(i+1));
                if (cur < next) {
                    sum += (next - cur);
                    i++;
                }else {
                    sum += cur;
                }
            }else {
                sum += cur;
            }
        }
        return sum;
    }

    public int getInt(char ch) {
        switch (ch) {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default:
                return 0;
        }
    }
}
